package com.ztmg.water.service.base.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import com.ztmg.common.util.resultUtils.JsonResult;
import com.ztmg.water.dto.base.ApartmentAreaDTO;
import com.ztmg.water.dto.base.ApartmentAreaPageDTO;
import com.ztmg.water.mapper.base.ApartmentAreaMapper;
import com.ztmg.water.model.base.ApartmentArea;
import com.ztmg.water.model.base.ApartmentAreaExample;
import com.ztmg.water.service.base.ApartmentAreaService;
import com.ztmg.water.vo.base.ApartmentAreaVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApartmentAreaServiceImpl implements ApartmentAreaService {
    @Resource
    private ApartmentAreaMapper apartmentAreaMapper;
    @Override
    public JsonResult create(ApartmentAreaDTO dto) {
        if(null != dto){
            ApartmentArea bean = new  ApartmentArea();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            apartmentAreaMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(ApartmentAreaDTO dto) {
        if(null != dto && null != dto.getId()){
            ApartmentArea bean = new ApartmentArea();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            bean.setModifier("");
            //后续完善
            apartmentAreaMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<ApartmentAreaVO> selectById(Integer id) {

        if(null != id){
            ApartmentArea bean = apartmentAreaMapper.selectByPrimaryKey(id);
            ApartmentAreaVO vo = new ApartmentAreaVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<ApartmentAreaVO>> select(ApartmentAreaDTO dto) {
        ApartmentAreaExample example = new ApartmentAreaExample();
        ApartmentAreaExample.Criteria criteria =  example.createCriteria();
        /***
         * 根据具体的业务，查询条件赋值
         */
        List<ApartmentArea> sysUserList = apartmentAreaMapper.selectByExample(example);

        List<ApartmentAreaVO> respDTOS = sysUserList.parallelStream().map((bean) -> {
            ApartmentAreaVO vo = new ApartmentAreaVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<ApartmentAreaVO>> selectPage(ApartmentAreaPageDTO dto) {
        ApartmentAreaExample example = new ApartmentAreaExample();
        ApartmentAreaExample.Criteria criteria =  example.createCriteria();
        if(StringUtils.isNotEmpty(dto.getSearchKey())){
            //根据送水人员名称查询
            criteria.andAreaNameLike("%"+dto.getSearchKey()+"%");
        }
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ApartmentArea> beanList = apartmentAreaMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<ApartmentAreaVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentAreaVO vo = new ApartmentAreaVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( apartmentAreaMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(ApartmentAreaDTO dto) {
        ApartmentAreaExample example = new ApartmentAreaExample();
        ApartmentAreaExample.Criteria criteria =  example.createCriteria();
        /***
         * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(apartmentAreaMapper.deleteByExample(example));
    }

    @Override
    public JsonResult batchDelete(List<Integer> ids) {
        ApartmentAreaExample example = new ApartmentAreaExample();
        ApartmentAreaExample.Criteria criteria =  example.createCriteria();
        criteria.andIdIn(ids);
        return JsonResult.success(apartmentAreaMapper.deleteByExample(example));
    }
}
